const Core = require('@alicloud/pop-core');
const rn = require("randomnumber");
// 使用jsonwebtoken存登陆状态
const jwt = require("jsonwebtoken");

module.exports = {
    // 生成发送信息的接口
    sendMsg(tel,code){
        var client = new Core({
            accessKeyId: 'LTAI4GKbFbtzL5DRCwbX7USR',
            accessKeySecret: 'KYarC3P1pNQdgcL9y9D0BmSOmau2Hw',
            endpoint: 'https://dysmsapi.aliyuncs.com',
            apiVersion: '2017-05-25'
          });
          
          var params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": tel,
            "SignName": "Sto螺蛳粉",
            "TemplateCode": "SMS_204440143",
            "TemplateParam": `{code:${code}}`
          }
          
          var requestOption = {
            method: 'POST'
          };
          
          return new Promise((resolve,reject)=>{
            client.request('SendSms', params, requestOption).then((result) => {
                console.log(JSON.stringify(result));
                resolve()
              }, (ex) => {
                console.log(ex);
                reject()
              })
          })
          
    },
    // 生成随机数信息的接口
    randomNum(n){
        // 参数n为生成几位的随机数 gennerate是randomnumber对象的方法
        return rn.generate(n)
    },
    // 对token的解析
    verify(t){
      return new Promise((resolve,reject)=>{
          jwt.verify(t,"千锋H5，牛！", (err,data)=>{
              if(err) reject();
              resolve(data);
          })
      })
  }

}